package bdz.projects.atwww.electrolux.model.core
{
	import flash.events.EventDispatcher;

	public class ELProgram extends EventDispatcher {
		private var _alias:String;
		private var _baseTime:Number;
		private var _options:ELOptions;
		private var _oxm:XML;
		private var _origBase:Number;
		public var _timeManaged:Boolean;
		private var _customTime:Number;
		private var _cachedTime:Number;
		public function ELProgram(xm:XML) {
			super();
			_oxm = xm;
			initialize(xm);
		}
		private function initialize(xm:XML):void{
			
			_alias = xm.@name;
			_origBase = _baseTime = Number(xm.@time) || 0;
			_options = new ELOptions(xm.options);
		}
		public function get alias():String{
			return _alias;
		}
		public function get options():ELOptions{
			return _options;
		}
		public function get cachedTime():Number{
			return _cachedTime;
		}
		public function get totalTime():Number{
			var optionTime:Number = 0;
			for each(var o:Object in _options.selectedOptions){
				try{
					optionTime += o.time;
				}catch(e:Error){}
			}
			_cachedTime = _baseTime + optionTime;
			return _cachedTime;
		}
		public function play():void{
			
		}
		
		private function serialize(al:String):XML{
			var xm:XML = new XML("<"+_oxm.name()+"/>");
			xm.@name = al;
			xm.@time = _origBase;
			xm.options = _options.serialized;
			return xm;
		}
		public function appendToBaseTime(n:Number):void{
			
			
			if(_timeManaged){
				_baseTime = n + (_customTime || 0);	
			}else{
				_baseTime = _origBase + n;
			}
		}
		public function revertToOriginalBase():void{
			_timeManaged = false;
			_baseTime = _origBase;
			_customTime = NaN;
			initialize(_oxm);
			
		}
		public function set customTime(n:Number):void{
			_timeManaged = true;
			_customTime = n;
		}
		public function get origTime():Number{
			return _origBase;
		}
		public function get timeManager():ELOption{
			for each(var _opt:ELOption in _options){
				if(!_opt.included){
					return _opt;
				}
			}
			return null;
		}
		public function set timeManaged(b:Boolean):void{
			_timeManaged = b;
		}
		public function clone(al:String):XML{
			return serialize(al);
		}
		
		
		
	}
}